import Vue from 'vue'
import Router from 'vue-router'
import {getToken,getUser} from '@/utils/auth.js'

Vue.use(Router)
// 初始化功能列表
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('./views/Home.vue'),
    redirect:'/main',
    children: [
      {
        path: '/main',
        component: () => import('./views/main/Main.vue')
      },
      {
        path: '/error',
        component: () => import('./views/error/Error.vue')
      },
      {
        path: '/test',
        component: () => import('./views/test/Test.vue')
      },
      {
        path: '/kzdata',
        component: () => import('./views/kz/Kzdata.vue')
      },
      {
        path: '/kzkf',
        component: () => import('./views/kz/Kzkf.vue')
      },
      {
        path: '/kzts',
        component: () => import('./views/kz/Kzts.vue')
      },
      {
        path: '/kztsnew',
        component: () => import('./views/kz/KzstNew.vue')
      },
      {
        path: '/KzProduct',
        component: () => import('./views/kz/KzProduct.vue')
      },
      {
        path: '/KzSale',
        component: () => import('./views/kz/KzSale.vue')
      },
      
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('./views/Login.vue')
  }
];

// 免登录白名单
const whiteList = ['/login', '/authredirect'] 

const router = new Router({
  routes
})

// 进入流程前的遍历逻辑
router.beforeEach((to, from, next) => {
  var token = getToken();
  console.log("token=====",token)
  if(token == null){
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      next('/login') // 否则全部重定向到登录页
    }
  }else{
    next()
  }
})

export default router // 导入 路由